/*
 *  Copyright 2010 sonrisa.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package com.zh.jbet.web.panels.context;

import com.zh.jbet.web.panels.teammatch.ContextTreeMatchPanel;
import com.zh.jbet.core.entity.GameContext;
import com.zh.jbet.web.JBetServiceProvider;
import com.zh.jbet.web.components.datatable.DataTablePanel;
import com.zh.jbet.web.components.datatable.ModalEditCloseListener;
import com.zh.jbet.web.components.datatable.TableColumnDescriptor;
import com.zh.jbet.web.menu.MenuConfig;
import com.zh.jbet.web.model.EntityDataProvider;
import com.zh.jbet.web.model.GameContextTreeModel;
import com.zh.jbet.web.panels.game.GameSub2Panel;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.StringResourceModel;

/**
 *
 * @author sonrisa
 */
@MenuConfig(context={ContextTreeEditPanel.class, ContextTreeMatchPanel.class, GameSub2Panel.class})
public class ContextTreeListPanel extends DataTablePanel<GameContext> {
    private static final TableColumnDescriptor[] COLS = new TableColumnDescriptor[] {
        new TableColumnDescriptor(TableColumnDescriptor.ColumnType.PROPERTY, "name", true),
        new TableColumnDescriptor(TableColumnDescriptor.ColumnType.PROPERTY, "shortName", true),
    };


    public ContextTreeListPanel(String id, IModel model) {
        super(id, COLS, new EntityDataProvider<GameContext>(JBetServiceProvider.CONTEXT_SERVICE), 10);
        add(new AjaxLink("newContext") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                final ContextEditModalPanel gcm = new ContextEditModalPanel(getJBetPage().getModal(), Model.of(new GameContext()), false);
                gcm.setEditCloseListener(new ModalEditCloseListener() {
                    @Override
                    public void onModalClose(AjaxRequestTarget target, boolean save, boolean delete) {
                        if (save) {
                            onRowSelected(target, (IModel<GameContext>)gcm.getDefaultModel());
                        }
                    }
                });
                gcm.show(target);
                
            }
        });
    }

    @Override
    protected IModel<String> createRowLinkModel() {
        return new StringResourceModel("link.details", null);
    }

    @Override
    protected void onRowSelected(AjaxRequestTarget target, IModel<GameContext> rowModel) {
        getJBetPage().changePanel(getDetailMenuConfig(), new GameContextTreeModel(rowModel.getObject()), getClass(), target);
    }

}
